package com.farm.wcp.controller;

import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.farm.authority.FarmAuthorityService;
import com.farm.authority.domain.User;
import com.farm.core.page.ViewMode;
import com.farm.core.sql.result.DataResult;
import com.farm.core.sql.result.ResultsHandle;
import com.farm.learn.domain.ClassHour;
import com.farm.learn.domain.Classt;
import com.farm.learn.domain.Major;
import com.farm.learn.service.ClasshourServiceInter;
import com.farm.learn.service.ClasstServiceInter;
import com.farm.learn.service.MajorServiceInter;
import com.farm.learn.service.MajorchapterServiceInter;
import com.farm.learn.service.MajorclassServiceInter;
import com.farm.learn.service.MajorpopServiceInter;
import com.farm.learn.service.UserclassServiceInter;
import com.farm.sfile.WdapFileServiceInter;
import com.farm.sfile.enums.FileModel;
import com.farm.social.domain.CommentsUser;
import com.farm.social.domain.EvaluationObj;
import com.farm.social.service.CommentsobjServiceInter;
import com.farm.social.service.EvaluationobjServiceInter;
import com.farm.social.service.FavoriteobjServiceInter;
import com.farm.util.web.HtmlUtils;
import com.farm.web.WebUtils;
import com.farm.wlp.events.EventHandLoader;
import com.farm.wuc.events.WucEventInfo;
import com.farm.wuc.events.WucEventModle;
import com.farm.wuc.events.WucEventRole;
import com.farm.wuc.events.WucEventUsers;
import com.wcp.question.service.QuestionServiceInter;

/**
 * 社交功能
 * 
 * @author autoCode
 * 
 */
@RequestMapping("/socialweb")
@Controller
public class SocialWebController extends WebUtils {
	@Resource
	private QuestionServiceInter questionServiceImpl;
	@SuppressWarnings("unused")
	private static final Logger log = Logger.getLogger(SocialWebController.class);
	@Resource
	private WdapFileServiceInter wdapFileServiceImpl;
	@Resource
	private UserclassServiceInter userClassServiceImpl;
	@Resource
	private MajorServiceInter majorServiceImpl;
	@Resource
	private MajorclassServiceInter majorClassServiceImpl;
	@Resource
	private MajorchapterServiceInter majorChapterServiceImpl;
	@Resource
	private MajorpopServiceInter majorPopServiceImpl;
	@Resource
	private FavoriteobjServiceInter favoriteObjServiceImpl;
	@Resource
	private CommentsobjServiceInter commentsObjServiceImpl;
	@Resource
	private EvaluationobjServiceInter evaluationObjServiceImpl;
	@Resource
	private ClasstServiceInter classTServiceImpl;
	@Resource
	private ClasshourServiceInter classHourServiceImpl;

	/**
	 * 收藏
	 * 
	 * @return
	 */
	@RequestMapping("/favorite")
	@ResponseBody
	public Map<String, Object> favorite(String appid, String apptype, HttpSession session, HttpServletRequest request) {
		try {
			favoriteObjServiceImpl.favorite(appid, apptype, getCurrentUser(session));
			runEvent(WucEventModle.FAVORITE, appid, apptype, null, null, session, request);
			return ViewMode.getInstance().returnObjMode();
		} catch (Exception e) {
			return ViewMode.getInstance().setError(e.getMessage(), e).returnObjMode();
		}
	}

	/**
	 * 记录收藏事件
	 * 
	 * @param modle
	 * @param appid
	 * @param apptype
	 * @param score    如果是评价录入评价得分
	 * @param comments 如果是有评论录入评论
	 * @param session
	 * @param request
	 */
	private void runEvent(WucEventModle modle, String appid, String apptype, Integer score, String comments,
			HttpSession session, HttpServletRequest request) {
		try {
			String objtitle = null;
			String appTypeName = null;
			String cuserid = null;
			// 1:专业,2:课程,3:课时
			if ("1".equals(apptype)) {
				Major major = majorServiceImpl.getMajorEntity(appid);
				if (major != null) {
					objtitle = major.getTitle();
					appTypeName = "专业";
					cuserid = major.getCuser();
				}
			}
			if ("2".equals(apptype)) {
				Classt classt = classTServiceImpl.getClasstEntity(appid);
				if (classt != null) {
					objtitle = classt.getName();
					appTypeName = "课程";
					cuserid = classt.getCuser();
				}
			}
			if ("3".equals(apptype)) {
				ClassHour hour = classHourServiceImpl.getClasshourEntity(appid);
				if (hour != null) {
					objtitle = hour.getTitle();
					appTypeName = "课时";
					cuserid = hour.getCuser();
				}
			}
			WucEventInfo info = new WucEventInfo(appid, objtitle).addText1(apptype, "类型").addText2(appTypeName, "类型名称");
			if (score != null) {
				info.addNum1(score, "评价分数");
			}
			if (StringUtils.isNotBlank(comments)) {
				info.addText3(comments, "评论内容");
			}
			if (StringUtils.isNotBlank(objtitle)) {
				EventHandLoader.getHandler().runEvent(//
						modle, //
						info, //
						new WucEventUsers()
								.addUser(WucEventRole.AUTHOR, FarmAuthorityService.getInstance().getUserById(cuserid),
										null)
								.addUser(WucEventRole.OPRETOR, getCurrentUser(session), getCurrentIp(request)));
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 取消收藏
	 * 
	 * @return
	 */
	@RequestMapping("/unfavorite")
	@ResponseBody
	public Map<String, Object> unfavorite(String appid, String apptype, HttpSession session) {
		try {
			favoriteObjServiceImpl.unFavorite(appid, getCurrentUser(session));
			return ViewMode.getInstance().returnObjMode();
		} catch (Exception e) {
			return ViewMode.getInstance().setError(e.getMessage(), e).returnObjMode();
		}
	}

	/**
	 * 发布评论
	 * 
	 * @param appid
	 * @param apptype
	 * @param parentid
	 * @param comments
	 * @param session
	 * @return
	 */
	@RequestMapping("/submitComments")
	@ResponseBody
	public Map<String, Object> submitComments(String appid, String apptype, String parentid, String comments,
			HttpSession session, HttpServletRequest request) {
		try {
			commentsObjServiceImpl.publicComments(appid, apptype, parentid, comments, getCurrentUser(session));
			runEvent(WucEventModle.COMMENT, appid, apptype, null, comments, session, request);
			return ViewMode.getInstance().returnObjMode();
		} catch (Exception e) {
			return ViewMode.getInstance().setError(e.getMessage(), e).returnObjMode();
		}
	}

	/**
	 * 加载评论(可多个appid用逗号分隔)
	 * 
	 * @param appid
	 * @param apptype
	 * @param session
	 * @return
	 */
	@RequestMapping("/PubloadComments")
	@ResponseBody
	public Map<String, Object> loadComments(String appids, Integer page, HttpSession session) {
		try {
			if (page == null) {
				page = 1;
			}
			DataResult result = commentsObjServiceImpl.loadComments(appids, page, 5);
			result.runformatTime("CTIME", "yyyy-MM-dd HH:mm");
			result.runHandle(new ResultsHandle() {
				@Override
				public void handle(Map<String, Object> row) {
					User user = (User) FarmAuthorityService.getInstance().getUserById((String) row.get("USERID"));
					Map<String, String> userMap = null;
					try {
						//移除敏感信息
						userMap = BeanUtils.describe(user);
						userMap.remove("password");
					} catch (Exception e) {
						e.printStackTrace();
					}
					row.put("user", userMap);
					row.put("IMGURL", wdapFileServiceImpl.getDownloadUrl(user.getImgid(), FileModel.PHOTO));
					row.put("NOTE", HtmlUtils.htmlEncode((String) row.get("NOTE")));
					if (row.get("PARENTID") != null) {
						CommentsUser parent = commentsObjServiceImpl.getUserComments((String) row.get("PARENTID"));
						if (parent != null) {
							row.put("parent", parent);
							row.put("parentNote", HtmlUtils.htmlEncode(parent.getNote()));
						}
					}
				}
			});
			return ViewMode.getInstance().putAttr("result", result).returnObjMode();
		} catch (Exception e) {
			return ViewMode.getInstance().setError(e.getMessage(), e).returnObjMode();
		}
	}

	/**
	 * 发布评价
	 * 
	 * @param appid
	 * @param apptype
	 * @param parentid
	 * @param comments
	 * @param session
	 * @return
	 */
	@RequestMapping("/submitEvaluation")
	@ResponseBody
	public Map<String, Object> submitEvaluation(String appid, String apptype, Integer score, String comments,
			HttpSession session, HttpServletRequest request) {
		try {
			evaluationObjServiceImpl.publicEvaluation(appid, apptype, score, comments, getCurrentUser(session));
			runEvent(WucEventModle.APPRAISE, appid, apptype, score, comments, session, request);
			return ViewMode.getInstance().returnObjMode();
		} catch (Exception e) {
			return ViewMode.getInstance().setError(e.getMessage(), e).returnObjMode();
		}
	}

	/**
	 * 加载评价
	 * 
	 * @param appid
	 * @param apptype
	 * @param session
	 * @return
	 */
	@RequestMapping("/PubloadEvaluation")
	@ResponseBody
	public Map<String, Object> loadEvaluation(String appid, Integer page, HttpSession session) {
		try {
			if (page == null) {
				page = 1;
			}
			DataResult result = evaluationObjServiceImpl.loadEvaluation(appid, page, 10);
			result.runformatTime("CTIME", "yyyy-MM-dd HH:mm");
			result.runHandle(new ResultsHandle() {
				@Override
				public void handle(Map<String, Object> row) {
					User user = (User) FarmAuthorityService.getInstance().getUserById((String) row.get("USERID"));
					row.put("IMGURL", wdapFileServiceImpl.getDownloadUrl(user.getImgid(), FileModel.PHOTO));
					row.put("user", user);
					row.put("NOTE", HtmlUtils.htmlEncode((String) row.get("NOTE")));
				}
			});
			EvaluationObj obj = new EvaluationObj();
			if (result.getResultList().size() > 0) {
				obj = evaluationObjServiceImpl
						.getEvaluationobjEntity((String) result.getResultList().get(0).get("OBJID"));
			} else {
				obj.setScore(0f);
			}
			return ViewMode.getInstance().putAttr("result", result).putAttr("OBJ", obj).returnObjMode();
		} catch (Exception e) {
			return ViewMode.getInstance().setError(e.getMessage(), e).returnObjMode();
		}
	}
}
